//
//  BSPictureView.m
//  BaiSi
//
//  Created by Sycamore on 16/5/3.
//  Copyright © 2016年 Sycamore. All rights reserved.
//

#import "BSPictureView.h"
#import "BSThemeItem.h"
#import <UIImageView+WebCache.h>
#import <SDImageCache.h>
#import "UIImage+Resize.h"
#import "BSImageManager.h"
#import <DACircularProgress/DACircularProgressView.h>
#import <DALabeledCircularProgressView.h>
#import "BSSeeBigPictureViewController.h"

@interface BSPictureView ()
@property (weak, nonatomic) IBOutlet UIImageView *gifView;
@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet UIImageView *pictureView;

@property (weak, nonatomic) IBOutlet UIButton *bigPictureButton;
@property (weak, nonatomic) IBOutlet DACircularProgressView *progressView;
@property (weak, nonatomic) DALabeledCircularProgressView *progressLabel;

@end
@implementation BSPictureView

+ (instancetype)pictureView
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]firstObject];
}

- (void)awakeFromNib
{
    _progressView.roundedCorners = YES;
    _progressView.trackTintColor = [UIColor lightGrayColor];
    _progressView.progressTintColor = [UIColor whiteColor];
    DALabeledCircularProgressView *progressLabel = [[DALabeledCircularProgressView alloc]initWithFrame:_progressView.bounds];
    [_progressView addSubview:progressLabel];
    _progressLabel = progressLabel;
    _progressLabel.progressLabel.textColor = [UIColor whiteColor];
    _progressLabel.progressLabel.font = [UIFont boldSystemFontOfSize:16];
    
    
    //从xib里加载的控件显示有问题，或者说约束有冲突的时候，可以用这个解决（IOS8以前，代码创建的约束默认会有autoResizingMask属性，需要自己手动设置为NO）
    self.autoresizingMask = UIViewAutoresizingNone;

}


- (void)setItem:(BSThemeItem *)item
{
    _item = item;
    
    //防止重用问题，将下载进度清零
    _progressLabel.progress = 0.0f;
    _progressLabel.progressLabel.text = @"0.0%%";
    _contentView.backgroundColor = [UIColor lightGrayColor];
    //设置图片,首先从磁盘里面去取

    [BSImageManager bs_setImageWithURL:[NSURL URLWithString:item.image0] placeholderImage:nil options:SDWebImageRetryFailed imageView:_pictureView progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        if (expectedSize < 0) {
            return ;
           
        }
        //获取下载完成比例
        CGFloat progress = receivedSize * 1.0 / expectedSize;
        _progressView.progress = progress;
        _progressLabel.progressLabel.text = [NSString stringWithFormat:@"%.1f%%",progress * 100];
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (!item.is_bigPicture) {
            return ;
        }
        //拉伸图片到原本的尺寸
        CGSize size;
        if (item.height > BSScreenHeight * 5) {
            _contentView.backgroundColor = [UIColor whiteColor];
            size = image.size;
        }
        else{
            size = CGSizeMake(BSScreenWidth, BSScreenWidth / item.width * item.height);
        }
        
        image = [UIImage resizeImage:image size:size];
        
        _pictureView.image = image;

        //将图片存进磁盘里，下次直接从磁盘里面取
        [[SDImageCache sharedImageCache]storeImage:image forKey:item.image0 toDisk:YES];
        
        

    }];
    
    
    //设置gif图标
    _gifView.hidden = !item.is_gif;
    
    _bigPictureButton.hidden = !item.is_bigPicture;
    
    //大图显示的处理
    if (item.is_bigPicture) {
        //设置模式为top后，图片会自动缩放，需要对图片进行拉伸处理
        _pictureView.contentMode = UIViewContentModeTop;
        _pictureView.clipsToBounds = YES;
    }
    else{
        _pictureView.contentMode = UIViewContentModeScaleToFill;
        _pictureView.clipsToBounds = NO;
    }
    
}

//点击查看大图按钮
- (IBAction)seeBigPicture:(id)sender {
    [self seeBigPicture];
}

//点击，查看大图
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self seeBigPicture];
}

//查看大图
- (void)seeBigPicture
{
    BSSeeBigPictureViewController *seeBigVc = [[BSSeeBigPictureViewController alloc]init];
    
    //传值
    seeBigVc.item = _item;
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:seeBigVc animated:YES completion:nil];
}

@end
